/*
 * @Author: Laputa
 * @Version: V0.0
 * @Date: 2023-05-31 14:49:57
 * @LastEditors: Laputa
 * @LastEditTime: 2023-12-06 16:19:27
 * @Description: This file provides firmware functions to manage the following
 *               functionalities of the EIC part.
 *
 *               MCU Software Reset
 *               Set Interrupt Priority Level
 *               Set EPT Interrupt Priority Level
 *               Interrupt Enable / Disable
 *
 * Copyright (c) 2023 by Levetop, All Rights Reserved.
 */

#include "lt168_eic.h"

/*******************************************************************************
 * Code
 ******************************************************************************/

/**
 * @brief MCU Software Reset function
 * @return {void}
 */
void EIC_System_Software_Reset(void)
{
    Bit_Set(EIC->ICSR, EIC_ICSR_SRST_MASK);
}


/**
 * @brief The lower priority value,the higher the priority.
 *               The actual value of priority level is the default(IRQ0:0,IRQ1:1,...) value plus (priority level value * 64)
 * @param {eic_interrupt_source_te} int_source: Interrupt Source ,where can be (EIC_IT_ADC,EIC_IT_QSPI0...EIC_IT_EPORT1)
 * @param {eic_interrupt_priority_level_te} level: Priority Level ,
 *        where can be (EIC_IT_Priority_0,EIC_IT_Priority_64,EIC_IT_Priority_128,EIC_IT_Priority_192).
 * @return {void}
 */
void EIC_Set_Interrupt_Priority_Level(eic_interrupt_source_te int_source, eic_interrupt_priority_level_te level)
{
    uint8_t group                = int_source / 4 *4;
    uint8_t index                = int_source % 4;

    *((uint32_t *)(&EIC->PLSR[group])) &= ~(0xFF << (index * 8));
    *((uint32_t *)(&EIC->PLSR[group])) |= ((level << 6) << (index * 8));
}

/**
 * @brief EPT interrupt¡¯s default priority value is -2.EPT interrupt priority value is (-2 + Priority_Level*64).
 *               For example, Priority_Level is 2,EPT interrupt priority value equal to (-2 + 2*64 = 126).
 * @param {eic_interrupt_priority_level_te} level: Priority Level ,
 *         where can be (EIC_IT_Priority_0,EIC_IT_Priority_64,EIC_IT_Priority_128,EIC_IT_Priority_192).
 * @return {void}
 */
void EIC_Set_EPT_Priority_Level(eic_interrupt_priority_level_te level)
{
    Bit_Clear(EIC->SYSPLSR, EIC_SYSPLSR_EPTPRI_MASK);
    Bit_Set(EIC->SYSPLSR, EIC_SYSPLSR_EPTPRI(level));
}

/**
 * @brief Module Global Interrupt Control
 * @param {eic_interrupt_source_te} int_source: Interrupt Source ,where can be (EIC_IT_ADC,EIC_IT_QSPI0...EIC_IT_EPORT1)
 * @param {FunctionState} state: where can be Enable,Disable.
 * @return {void}
 */
void EIC_Cmd_Interrupt(eic_interrupt_source_te int_source, FunctionState state)
{
    Bit_Clear(EIC->IER, (1 << int_source));
    Bit_Set(EIC->IER, (state << int_source));
}

/**
 * @brief Close Chip All Module Interrupt
 * @return {void}
 */
void EIC_Close_ALL_Module_Interrupt(void)
{
    EIC->IER = 0;
}


/*!<---------------End of Code--------------->!*/
